<template>
  <vxe-modal v-model="secReportDialog" title="二级报告发布历史" :mask="false" width="70vw" height="85vh" show-footer :destroy-on-close="true" show-zoom resize>
    <template #default>
      <vxe-toolbar>
        <template #buttons>
          <vxe-radio-group v-model="searchCondition" @change="queryPublisHistory">
            <vxe-radio-button :label="1" content="全部"></vxe-radio-button>
            <vxe-radio-button :label="2" content="未取消"></vxe-radio-button>
            <vxe-radio-button :label="3" content="已取消"></vxe-radio-button>
          </vxe-radio-group>
          <vxe-button type="text" class="button-blue margin-left-xs" content="取消发布" @click="cancelSecReport" :loading="saveFirstReportLoading"></vxe-button>
        </template>
      </vxe-toolbar>
      <div style="height: calc(100% - 40px)">
        <vxe-table ref="tableRef" border show-overflow height="auto" :loading="loading" :row-config="{isHover: true,isCurrent:true}" :data="tableData" :scroll-y="{enabled: true}">
          <vxe-column type="seq" width="30"></vxe-column>
          <vxe-column field="isCancel" title="是否取消" width="60px" align="center">
            <template #default="{ row }">
              <span v-if="row.isCancel === 1" class="bg-red light padding-lr-xs radius">是</span>
              <span v-else class="bg-green light padding-lr-xs radius">否</span>
            </template>
          </vxe-column>
          <vxe-column field="stainingResultDto.strainName" title="菌株编号" width="110px"></vxe-column>
          <vxe-column field="stainingResultDto.stainingMethNa" title="染色方法" width="90px"></vxe-column>
          <vxe-column field="stainingResultDto.stainingConcNa" title="结论选项" min-width="80px"></vxe-column>
          <vxe-column field="stainingResultDto.stainingConcOptNa" title="选项结果" width="80px"></vxe-column>
          <vxe-column field="stainingResultDto.descText" title="描述" width="80px"></vxe-column>
          <vxe-column field="stainingResultDto.isAlarm" title="危急值" width="80px" align="center">
            <template #default="{ row }">
              <span v-if="row.stainingResultDto&&row.stainingResultDto.isAlarm == 1" class="bg-red light padding-lr-xs radius">{{ `是` }}</span>
              <span v-else>{{ `否` }}</span>
            </template>
          </vxe-column>
          <vxe-column field="createTime" title="发布时间" width="110px"></vxe-column>
        </vxe-table>
      </div>
    </template>
    <template #footer>
      <div class="flex justify-end" style="margin-top: 20px">
        <vxe-button type="text" class="button-white margin-left-xs" content="取消" @click="secReportDialog = false"></vxe-button>
      </div>
    </template>
  </vxe-modal>
</template>
<script lang="ts" setup>
import { reactive, ref, defineExpose, getCurrentInstance } from 'vue';
import { IsEmpty, sortByField, formatDateTime } from '@/utils/common';
import { VxeMessage, VxeNotifyType } from '@/utils/CommonMessage';
import { getOptionValueByCodeGlobalOption, getOptionValueByCodeRptGroupOption } from '@/utils/SysOptionsHelper';
import { Splitpanes, Pane } from 'splitpanes';
import { VXETable } from 'vxe-table';
import { saveProcessLogs } from '@/utils/BacProcessLogHelper';


let { proxy }: any = getCurrentInstance();
let bloodCultureObj = reactive({});
let mainInfo = reactive({}); //主记录病人信息
let secReportDialog = ref(false); /**控制弹窗显示与否 */
let curCulMediumObj = reactive({}); /**当前选中节点 */
let curStrainObj = reactive({});/* 当前选中的菌株 */
let saveFirstReportLoading = ref(false); /**保存初级报告 */
let allowEditRptTime = ref(false); /**允许编辑上报时间 */
let tableData = reactive([]);/**表格对象 */
let loading = ref(false);/* 遮罩层 */
let searchCondition = ref(1);/* 查询条件 */
const tableRef = ref(null);/* 表格对象 */
defineExpose({
  initPage
});

function initPage(labMainInfo, formData, culmdmObj, currentStainResult) {
  secReportDialog.value = true;
  Object.assign(mainInfo, labMainInfo);
  Object.assign(bloodCultureObj, formData);
  Object.assign(curCulMediumObj, culmdmObj);
  Object.assign(curStrainObj, currentStainResult);
  queryPublisHistory();
}
/* 查询发布历史 */
function queryPublisHistory() {
  loading.value = true;
  tableData.length = 0;
  const url = '/api/bac/bacPublisSecReport/getBacPublisSecReports';
  const params = {
    rptGroup: mainInfo.rptGroup,
    sampleda: mainInfo.sampleda + ' 00:00:00',
    sampleNo: mainInfo.sampleNo,
    testFlowNo: mainInfo.testFlowNo,
    testNodeNo: mainInfo.nodeNo,
    strainNo: curStrainObj.strainNo,
    searchType: searchCondition.value
  }
  proxy.$http
    .post(url, params)
    .then((res: any) => {
      Object.assign(tableData, res.data);
      loading.value = false;
    })
    .catch((err: any) => {
      loading.value = false;
      VxeMessage('查询失败', VxeNotifyType.error);
    });
}
/* 取消发布 */
async function cancelSecReport() {
  const row = tableRef.value?.getCurrentRecord();
  if (!row) {
    VxeMessage('请选择要发布的记录', VxeNotifyType.warning);
    return;
  }
  const type = await VXETable.modal.confirm('是否确认取消发布选中的数据项？');
  if (type === 'confirm') {
    const url = '/api/bac/bacPublisSecReport/cancelPublisSecReport';
    proxy.$http
      .post(url, row)
      .then((res: any) => {
        queryPublisHistory();
        VxeMessage(res.status.msg, VxeNotifyType.success);
        try {
          /* 记录过程记录 */
          const a = localStorage.getItem('userInfo');
          const b = JSON.parse(a);
          const cntStr = [
            row && row.stainingResultDto && !IsEmpty(row.stainingResultDto.strainName) ? '菌株:' + row.stainingResultDto.strainName + '' : '',
            row && row.stainingResultDto && !IsEmpty(row.stainingResultDto.descText) ? '采样部位:' + row.stainingResultDto.descText + '' : '',
          ].filter(Boolean) // 过滤掉空字符串
            .join('\n');
          const strainNameStr = row && row.stainingResultDto && !IsEmpty(row.stainingResultDto.strainName) ? '菌株:' + row.stainingResultDto.strainName + '' : '';
          const processLogData = {
            strainId: null,
            strainNo: null,
            strainName: strainNameStr,
            maininfoId: mainInfo.id,
            wkFlwId: null,
            wkFlwNodeId: null,
            rptGroup: mainInfo.rptGroup,
            sampleda: formatDateTime(mainInfo.sampleda, 'yyyy-MM-dd 00:00:00'),
            sampleNo: mainInfo.sampleNo,
            testFlowNo: mainInfo.testFlowNo,
            testNodeNo: row.stainingResultDto.testNodeNo,
            content: `【${formatDateTime(new Date(), 'yyyy-MM-dd hh:mm:ss')}】操作人:【${IsEmpty(b.realname) ? '' : b.realname}】取消发布二级报告\n${cntStr}`,
            operatorDoctor: IsEmpty(b.username) ? '' : b.username
          };
          saveProcessLogs(processLogData);
        }
        catch (err) { }
      })
      .catch((err: any) => {
        VxeMessage(err, VxeNotifyType.error);
      });
  }
}
</script>

<style lang="scss" scoped>
:deep(.vxe-modal--wrapper) {
  z-index: 10 !important;
}
:deep(.el-descriptions__label) {
  width: 120px;
}
:deep(.el-descriptions__content) {
  color: #000;
}
:deep(.vxe-cell) {
  padding-left: 5px;
}
.tableForm {
  height: calc(100% - 80px);
  .tableBody {
    height: 75%;
  }
  .formBody {
    height: 25%;
    padding: 0 25px;
    :deep(.vxe-form--item) {
      margin-right: 15px;
    }
  }
}
/* 定义父容器的样式 */
.grid-container {
  display: grid;
  /* 行列的间距（可根据需要调整） */
  gap: 2px;
  /* 根据需要设定最小的列宽和行高 */
  ///min-width: 10px;
  //min-height: 10px;
  //grid-template-columns: repeat(3, 1fr);
  /* 自动计算列数和行数 */
  grid-template-columns: repeat(10, auto);
  //grid-template-rows: repeat(auto-fit, minmax(min-height, auto));
}

/* 定义子项的样式 */
.grid-item {
  /* 这里设置了子项的基础样式 */
  background-color: #e2e2e2; //#4caf50
  color: white;
  text-align: center;
  padding: 2px;
  border-radius: 0px;
  cursor: pointer;
  //font-size: clamp(0.1rem, 0.5rem, 2rem); /* 根据容器大小动态调整字体大小 */
  //text-align: center;
}
.active {
  background-color: #4caf50;
}
</style>
